package org.pragmatic.paint.project;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;

import org.pragmatic.paint.shapes.Oval;
import org.pragmatic.paint.shapes.Rectangle;
import org.pragmatic.paint.shapes.Shape;
import org.pragmatic.paint.shapes.Triangle;

public class Canvas extends JPanel{
	private List<Shape> shapes = new ArrayList<Shape>();
	private Shape drawShape;
	
	public Canvas(){
		setBackground(Color.WHITE);
		
		this.addMouseListener(new MouseAdapter() {
			
			@Override
			public void mousePressed(MouseEvent e) {
				drawShape = getTemplateShape(e.getX(), e.getY(), e.getX() + 1, e.getY() + 1); 
				shapes.add(drawShape);
			}
		});
		
		this.addMouseMotionListener(new MouseMotionAdapter() {
			
			@Override
			public void mouseDragged(MouseEvent e) {
				drawShape.setWidth(e.getX()-drawShape.getX());
				drawShape.setHeight(e.getY()-drawShape.getY());
				
				
				Canvas.this.repaint();
			}
		});
	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		
		for(Shape shape : shapes){
			shape.draw(g);
		}

	}

	public Shape getTemplateShape(int x, int y, int width, int height) {
		switch(MainFrame.shapeType){
		case "rectangle":
			return new Rectangle(x, y, width, height, MainFrame.shapeColor, MainFrame.strokeType);
		case "oval":
			return new Oval(x, y, width, height, MainFrame.shapeColor, MainFrame.strokeType);
		case "triangle":
			return new Triangle(x, y, width, height, MainFrame.shapeColor, MainFrame.strokeType);
			
		default:
			return new Rectangle(x, y, width, height, MainFrame.shapeColor, MainFrame.strokeType);
		}
	}

	
}
